package com.mf.page;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

import com.mf.R;

public class ActionActivity extends Activity  {
	//用来复位倒计时计数器的广播
	public static final String RESET_TIMER = "com.ActionActivity.ResetTimer";
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Exit(IPageCallback.CANCEL, "");
	}
	
	protected void OnTimerOut()
	{
		// TODO Auto-generated method stub
		Exit(IPageCallback.TIME_OUT, "");		
	}
	
	//保存退出原因，并退出
	protected void Exit(int result, Object input)
	{
		IPageCallback btnProc = (IPageCallback)this.getIntent().getExtras().getSerializable("pageCallback");

		if(btnProc != null)
		{
			btnProc.SetExitReason(result);
			btnProc.SetInputText(input);
		}
		
		finish();
	}
	
	int m_TimeBase;
	int m_TimeOut;
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if( item.getItemId() == R.id.action_cancel )
		{
			onBackPressed();
		}
		return super.onOptionsItemSelected(item);
		
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		registerReceiver(broadcastReceiver, new IntentFilter(RESET_TIMER));	
		initBaseParam(this.getIntent().getExtras() );
	}
	
	
	Timer m_timer;
	TimerTask m_task;
	void startTimer()
	{
		if( m_timer == null)
		{
			m_timer = new Timer();			
		}		
		if(m_task == null)
		{
			m_task = new TimerTask(){
				@Override
				public void run() {
					// TODO Auto-generated method stub
					if( m_TimeOut > 0 )
					{
						m_TimeOut--;	
						if( m_TimeOut == 0)
						{
							OnTimerOut();
						}
					}
				}				
			};
		}
		
		
		m_timer.schedule(m_task, 1000,1000);	
	}
	
	public void stopTimer()
	{
		if (m_timer != null) {  
			m_timer.cancel();  
			m_timer = null;  
        }  
  
        if (m_task != null) {  
        	m_task.cancel();  
        	m_task = null;  
        }
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		stopTimer();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		resetTimer();
	}

	public void resetTimer()
	{
		stopTimer();
		m_TimeOut = m_TimeBase;	
		startTimer();
	}


	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		stopTimer();	
		unregisterReceiver(broadcastReceiver);
	}
	
	protected void initBaseParam(Bundle param)
	{
		Log.v("uninpay", "ActionActivity onRefrushParam");
		if( param != null )
		{
			String title = param.getString("title");
			if( title != null)
			{
				setTitle(title);		
			}
			m_TimeBase =  param.getInt("timeout");		
			if( m_TimeBase == 0)
			{
				m_TimeBase = 60;
			}			
		}
		
		Log.v("uninpay", "ActionActivity onRefrushParam 1");
		
		resetTimer();
	}
	
	protected void onRefrushParam(Bundle param) 
	{
		initBaseParam(param);
	}

	public void showError(String msg)
	{
		Toast.makeText(this.getApplicationContext(), msg, 3000);
	}
	
	 /*
     * 接收动态注册广播的BroadcastReceiver，比如用来复位倒计时计数器
     */
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
        	if(action == RESET_TIMER)
        	{	//复位倒计时计数器
        		resetTimer();
        	}
        }
    };
}
